import { createApp } from 'vue'
import { createPinia } from 'pinia'
import ElementPlus from 'element-plus'
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
import 'element-plus/dist/index.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import router from './router'
import App from './App.vue'
import './assets/main.css'
import './styles/responsive.css'
import './styles/table.css'
import permission from './directives/permission'
import configService from './utils/configService'

// 异步初始化应用
async function initApp() {
  try {
    // 等待配置服务初始化完成
    await configService.init()
    console.log('配置服务初始化完成，开始挂载应用')
  } catch (error) {
    console.error('初始化配置服务失败:', error)
    // 即使配置服务初始化失败，也要继续挂载应用
  }

  // 创建并挂载应用
  const app = createApp(App)

  // 注册所有图标
  for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
  }

  // 注册全局指令
  app.directive('permission', permission)

  app.use(createPinia())
  app.use(router)
  app.use(ElementPlus, {
    locale: zhCn
  })

  app.mount('#app')
}

// 启动应用
initApp()